﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FResultVoteWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FResultVoteWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }
        public override string Title
        {
            get
            {
                return GetText("Title", "Kết quả thăm dò ý kiến");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("DefaultView",GetText("DefaultView","Result Vote"),0)
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FResultVoteWP";
            }
        }

        private string ClientRenderWebPartRight(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            string Today = DateTime.Now.ToLocalTime().ToString();
            string VoteId = System.Web.HttpContext.Current.Request["VoteId"];
            VoteItemCls[] VoteItems = null;
            string Question = new VoteBLL().GetInfo(Environments.ActiveLanguage, VoteId, ref VoteItems);
            int TotalVote = new VoteBLL().GetTotal(VoteId);
            string Html =
                    "<table width=100% cellspacing=0 cellpadding=0 border=1 id=AdVote style=display :none background-color:none;>" +
                    "<tr><td>" +
                    "<table width=100% cellspacing=0 cellpadding=0 border=1>" +
                    "<tr><td class=VoteRemark><h3>" + "Sở y tế Nghệ An (" + Today + ")</h3></td></tr>" +
                    "<tr><td class=voteQuestion><h1><b><i>" + Question + "</i></b></h1></td></tr>" +
                    "</table></br>";
            Html +=
                "<table border=0 cellspacing=1 cellpadding=3 width=100%>" +
                "<tr><th><b>Trả lời</b></th><th>Tỉ lệ</th><th>Số phiếu</th></tr>";
            double temp = 0;
            for (int iIndex = 0; iIndex < VoteItems.Length - 1; iIndex++)
            {
                Html +=
                    "<tr height=30>" +
                    "<td  class=VoteShow><h4>" + VoteItems[iIndex].VoteItemName + "</h4></td>" +
                    "<td width=" + TotalCountC(VoteItems[iIndex].Total, TotalVote) + "%><img src = Images/Vote/Imagevote" + iIndex + ".jpg  height = 10px width = " + TotalCount(VoteItems[iIndex].Total, TotalVote) + "% border = 0>" + TotalCountC(VoteItems[iIndex].Total, TotalVote) + "%</td>" +
                    "<td class=VoteCount>" + VoteItems[iIndex].Total + " Phiếu</span></td>" +
                    "</tr>";
                temp += TotalCountC(VoteItems[iIndex].Total, TotalVote);
            }
            double temp1 = 100.00 - temp;
            Html +=
                "<tr height=30>" +
                "<td  class=VoteShow><h4>" + VoteItems[VoteItems.Length - 1].VoteItemName + "</h4.</td>" +
                "<td width=" + temp1 + "%><img src = Images/Vote/Imagevote" + (VoteItems.Length - 1) + ".jpg  height = 10px width = " + temp1 + "% border = 0>" + temp1 + "%</td>" +
                "<td class=VoteCount>" + VoteItems[VoteItems.Length - 1].Total + " Phiếu</span></td>" +
                "</tr>";
            Html += "</table>";
            Html += "<table width=100% cellspacing=0 cellpadding=0 border=0>" +
                    "<tr>" +
                    "<td height=20 valign=bottom align=left><a href=JavaScript:window.close() class=Time><font style:bold>[Trở lại]</font></a></td>" +
                    "</tr>" +
                    "</table>";
            Html += "</td></tr></table>";
            return Html;
        }
        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            if (ViewName.Equals("DefaultView"))
            {
                return ClientRenderWebPartRight(TabModuleId, HasPorlet);
            }
            return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
        }

        private float TotalCount(float pValue, float Total)
        {
            float totalCount;
            if (Total == 0 || pValue == 0)
            {
                totalCount = 5;
            }
            else
            {
                totalCount = (float)(pValue / Total) * 100;
            }

            return Math.Abs(totalCount);
        }

        private double TotalCountC(float pValue, float Total)
        {
            double totalCountC = 0;
            if (Total == 0 || pValue == 0)
            {
                totalCountC = 0;
            }
            else
            {
                totalCountC = (pValue / Total) * 100;
            }
            return Math.Ceiling(totalCountC);
        }



        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "69EB07FC-EA01-46fe-A1B3-5BDA611C7111","ResultVote"
            });
            return dtResult;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.FResultVoteWP), ActivePage);
        }
    }
}
